package com.livrexpress.livrexpressandroid.localdomain;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.livrexpress.livrexpressandroid.localdomain.DataManager;

public class Param implements Entity{
    private static String TABLE = "Param";
    private static String COL_KEY = "key";
    private static int COL_KEY_NUM = 0;
    private static String COL_VALUE = "value";
    private static int COL_VALUE_NUM = 1;
    
    
    private String key;
    private String value;
    
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void createTable(SQLiteDatabase db) {
        String CREATE_BDD = "CREATE TABLE " + TABLE + " ("
	+ COL_KEY + " TEXT PRIMARY KEY, "
        + COL_VALUE + " TEXT NOT NULL);";
        db.execSQL(CREATE_BDD);
    }
    
    public void removeTable(SQLiteDatabase db)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE);
    }
    
    public void insert() {
        ContentValues val = new ContentValues();
        val.put(COL_KEY, key);
        val.put(COL_VALUE, value);
        
        LivreApplication.getSqliteManager().insert(TABLE, val);
    }

    public void update() { 
        ContentValues val = new ContentValues();
        val.put(COL_VALUE,this.value);
        LivreApplication.getSqliteManager().update(TABLE, val, COL_KEY+"= '"+this.key +"'");
    }

    public void delete() {
              
    	LivreApplication.getSqliteManager().delete(TABLE, COL_KEY + "='" +this.key+"'");
    }
    
    public static Param getParam(String key)
    {
        Cursor result;
        try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM " + TABLE + 
                                                                    " WHERE " + COL_KEY + " = '" + key +"'" , null);
        }
        catch(Exception e)
        {
            return null;
        }
        if(!result.moveToNext())
        {
            result.close();
            return null;
        }
        Param param = new Param();
        param.setKey(key);
        param.setValue(result.getString(COL_VALUE_NUM));
        result.close();
        return param;    
    }

    
}

